﻿using MVC_No09.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_No09.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index(string type, string text)
        {
            using (Model1 model = new Model1())
            {
                #region 查询
                var list = model.ProInfo.Where(u => true).ToList();
                if (int.TryParse(type, out int i))
                {
                    var typeId = Convert.ToInt32(type);
                    if (typeId != 0)
                        list = list.Where(u => u.Type == typeId).ToList();
                }
                if (!string.IsNullOrEmpty(text))
                {
                    list = list.Where(u => u.Name.IndexOf(text) >= 0).ToList();
                }
                #endregion

                #region 类型名称
                Dictionary<int, string> dict = new Dictionary<int, string>();
                var proType = model.ProType.Where(u => true).ToList();
                foreach (var item in proType)
                    dict.Add(item.ID, item.Name);
                ViewBag.proTypeList = dict;
                #endregion

                #region 物资类型下拉框
                var typeList = model.ProType.Where(u => true).ToList();
                List<SelectListItem> typeListAsItem = new List<SelectListItem>();
                typeListAsItem.Add(new SelectListItem() { Value = "0", Text = "全部", Selected = true });
                foreach (var item in typeList)
                    typeListAsItem.Add(new SelectListItem() { Value = item.ID.ToString(), Text = item.Name });
                SelectList selectListItems = new SelectList(typeListAsItem, "Value", "Text", "Selected");
                ViewBag.TypeList = selectListItems;
                #endregion

                return View(list);
            }
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            if (!int.TryParse(id, out int i)) return Json(3);
            using (Model1 model = new Model1())
            {
                var uid = Convert.ToInt32(id);
                var a = model.ProInfo.FirstOrDefault(u => u.ID == uid);
                a.IsDestroy = true;

                return Json(model.SaveChanges());
            }
        }

        [HttpGet]
        public ActionResult Create()
        {
            using (Model1 model = new Model1())
            {
                #region 下拉列表类型
                List<SelectListItem> typeList = new List<SelectListItem>();
                foreach (var item in model.ProType.Where(u => true).ToList())
                    typeList.Add(new SelectListItem() { Value = item.ID.ToString(), Text = item.Name });
                ViewBag.TypeList = new SelectList(typeList, "Value", "Text");
                #endregion
            }
            return View(new ProInfo());
        }

        [HttpPost]
        public ActionResult Create(ProInfo proInfo)
        {
            if (ModelState.IsValid) {
                using (Model1 model = new Model1()) {
                    proInfo.IsDestroy = false;
                    model.ProInfo.Add(proInfo);
                    return RedirectToAction("Index","Home");
                }
            }
            return Json("");
        }
    }
}